import bundle2name.BaiduPP
import bundle2name.FilePipeline
import bundle2name.PP
import bundle2name.baiduUrls
import org.apache.xpath.XPath
import org.jsoup.Jsoup
import us.codecraft.webmagic.Page
import us.codecraft.webmagic.Spider
import us.codecraft.webmagic.selector.Html
import us.codecraft.xsoup.Xsoup
import java.nio.file.Files
import java.nio.file.Paths
import java.util.*

fun main(args: Array<String>) {
    /* val con = Jsoup.connect("https://www.jd.com/")
     val doc = con.get()
     val cmi = doc.select(".cate_menu_item")
     var i = 1
     cmi.forEach { e ->
         val top = e.text()
         val cateDetail = doc.select("#cate_item${i++} .cate_detail").first()
         val cates = cateDetail.getElementsByTag("dl")
         cates.forEach { e ->
             val l2 = e.child(0).text()
             e.child(1).children().forEach { e ->
                 println("$top \t $l2 \t ${e.text()}")
             }
         }
     }*/

    val map = TreeMap<String, MutableMap<String, MutableList<String>>>()

    val s = String(Files.readAllBytes(Paths.get("f:\\jd.html")))
    val html = Html(s, "")
    val cmi = html.`$`(".cate_menu_item")
    var i = 1
    cmi.nodes().forEach { e ->
        val top = e.xpath("//a/text()").all().joinToString("/")
        val cates = html.xpath("//div[@id='cate_item${i++}']//div[@class='cate_detail']/dl")
        cates.nodes().forEach { e ->
            val l2 = e.xpath("//dt/a/text()").get()
            e.xpath("//dd/a/text()").all().forEach { s ->
                val ll = map.computeIfAbsent(top, { mutableMapOf() })
                l2?.let {
                    val lll = ll.computeIfAbsent(it, { mutableListOf() })
                    lll.add(s)
                }
            }
        }
    }
    val icm = mutableMapOf<String, IC>()
    var code = 10000
    var id = 1
    map.forEach { l1, _ ->
        println("""
            insert into Category (id,code,level,name,position,status,created,updated) values($id,"$code",1,"$l1",1,1,now(),now());
        """.trimIndent())
        icm.put(l1, IC(id, code))
        id++
        code += 10000

    }

    map.forEach { l1, m ->
        val ic = icm[l1]!!
        val pid = ic.id
        var code = ic.code + 100
        m.forEach { l2, _ ->
            println("""
            insert into Category (id,code,level,name,position,status,parentId,created,updated) values($id,"$code",2,"$l2",2,1,$pid,now(),now());
        """.trimIndent())
            icm.put("$l1$l2",IC(id,code))
            code += 100
            id++
        }

    }

    map.forEach { l1, m ->
        m.forEach { l2, cs ->
            val ic = icm["$l1$l2"]!!
            val pid = ic.id
            var code = ic.code + 1
            cs.forEach {
                println("""
            insert into Category (id,code,level,name,position,status,parentId,created,updated) values(${id++},"${code++}",3,"$it",1,1,$pid,now(),now());
        """.trimIndent())
            }

        }

    }
}

data class IC(val id: Int, val code: Int)